import redis
from contextlib import contextmanager
from backend.settings_base import *

# 配置uwsgi支持dwebsocket
WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'

DEBUG = False

LOGGING['loggers']['django']['handlers'] = ['file']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'backend',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'mysql',
        'PORT': '3306',
    }
}

# 设置redis缓存。这里密码为redis.conf里设置的密码
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://redis:6379/0",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "PASSWORD": "123456",
        },
    }
}


@contextmanager
def REDIS_CONNECT():
    redis_conn = redis.Redis(host='redis', password='123456')
    yield redis_conn
    redis_conn.close()


class REDIS_PUB_SUB:

    def __init__(self, channel_name):
        self.__conn = redis.Redis(host='redis', password='123456')
        self.channel_name = channel_name

    def public(self, msg):
        """
        在指定频道上发布消息
        :param msg:
        :return:
        """
        self.__conn.publish(self.channel_name, msg)
        return True

    def subscribe(self):
        pub = self.__conn.pubsub()
        pub.subscribe(self.channel_name)
        return pub


URL_MSG = {
    "create_staion": 'http://47.106.68.206:7301/station/createStationThroughESS',
}
